package com.yiwenup.leetcode.top_interview_150;

import com.yiwenup.leetcode.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.top_interview_150
 * @Author yiwenup
 * @Date 2024-12-16 18:58:12
 * @Description https://leetcode.cn/problems/reverse-linked-list-ii/description/?envType=study-plan-v2&envId=top-interview-150
 */
public class No0092 {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode cur = head;
        List<ListNode> tmp = new ArrayList<>();
        while (cur != null) {
            tmp.add(cur);
            cur = cur.next;
        }

        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;

        ListNode p = dummyHead;
        for (int i = 0; i < left - 1; i++) {
            p.next = tmp.get(i);
            p = p.next;
        }

        for (int i = right - 1; i >= left - 1; i--) {
            p.next = tmp.get(i);
            p = p.next;
        }

        for (int i = right; i < tmp.size() ; i++) {
            p.next = tmp.get(i);
            p = p.next;
        }
        p.next = null;

        return dummyHead.next;
    }
}
